package com.xqboss.apps.service.consumer;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xqboss.apps.domain.consumer.ConsumerInteractMsg;
import com.xqboss.apps.dto.consumer.ConsumerInteractMsgDto;
import com.xqboss.apps.enums.consumer.InteractMsgEnum;
import com.xqboss.apps.mapper.consumer.ConsumerInteractMsgMapper;
import com.xqboss.apps.vo.consumer.ConsumerInteractMsgVo;
import com.xqboss.common.exception.ServiceException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


/**
 * 互动消息表接口
 *
 * @author wangcong
 * @date 2023-08-31
 */
@Service
public class ConsumerInteractMsgService extends ServiceImpl<ConsumerInteractMsgMapper, ConsumerInteractMsg> {

    @Resource
    private ConsumerInteractMsgMapper consumerInteractMsgMapper;


    public List<ConsumerInteractMsgVo> listPage(ConsumerInteractMsgDto dto) {
        LambdaQueryWrapper<ConsumerInteractMsg> wq = new LambdaQueryWrapper<>();
        wq.eq(ConsumerInteractMsg::getUserId, dto.getUserId());
        wq.orderByDesc(ConsumerInteractMsg::getCreateTime);
        List<ConsumerInteractMsg> list = list(wq);

        List<ConsumerInteractMsgVo> vos = BeanUtil.copyToList(list, ConsumerInteractMsgVo.class);
        return vos;
    }

    @Transactional(rollbackFor = Exception.class)
    public void record(Long userId, String content, Long sourceId, InteractMsgEnum msgEnum) {
        ConsumerInteractMsg msg = new ConsumerInteractMsg();
        msg.setUserId(userId);
        msg.setContent(content);
        msg.setSourceId(sourceId);
        msg.setType(msgEnum);
        Assert.isTrue(this.save(msg),()->new ServiceException("互动消息记录失败~"));
    }
}
